//
//  MessageRemind.m
//  Messenger
//
//  Created by OC on 15/6/9.
//  Copyright (c) 2015年 YN-APP-iOS. All rights reserved.
//

#import "MessageRemind.h"
#import "YNIMFrameworkMacro.h"
#import "YNContactGroup.h"
#import "FMDatabase.h"
#import "FMDatabaseQueue.h"
#import "FMDatabaseAdditions.h"
#import "YNIMTool.h"

@implementation MessageRemind

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if (!value) {
        if ([key isEqualToString:@"isRemind"]) {
            self.isRemind=0;
        }
    }
}

+ (NSString *)groupID
{
  return @"groupID";
}

+ (NSString *)groupName
{
  return @"groupName";
}

+ (NSString *)isRemind
{

  return @"isRemind";
}

+ (NSString *)TableName
{
  return @"MessageRemind";
}

- (NSArray *)insertParam
{
    return @[[YNIMTool nilFilter:self.groupID],
             [YNIMTool nilFilter:self.groupName],
             [NSNumber numberWithInteger:self.isRemind]];

}

- (NSArray *)updateParam
{
    return @[[YNIMTool nilFilter:self.groupName],
             [NSNumber numberWithInteger:self.isRemind],
             [YNIMTool nilFilter:self.groupID]];
}

//创建群/讨论组消息提醒表
+ (BOOL)createInDatabase:(FMDatabase *)db
{
    if ([db tableExists:[MessageRemind TableName]])
    {
        return YES;
    }
    
    NSString *createTableSql = [NSString stringWithFormat:@"%@%@(%@ varchar(36) NOT NULL PRIMARY KEY UNIQUE, %@ varchar(36),%@ INTEGER)",CREATE_TABLE,
                                [MessageRemind TableName],
                                [MessageRemind groupID],
                                [MessageRemind groupName],
                                [MessageRemind isRemind]];

    BOOL result = [db executeUpdate:createTableSql];
    
    return result;
}


- (BOOL)insertInDatabase:(FMDatabase *)db
{
    NSString *fieldString = [NSString stringWithFormat:@"%@,%@,%@",
                             [MessageRemind groupID],
                             [MessageRemind groupName],
                             [MessageRemind isRemind]
                             ];

    NSString *insertTableSql = [NSString stringWithFormat:@"%@%@ (%@) VALUES (?,?,?)",INSERT_TABLE,[MessageRemind TableName],fieldString];
    BOOL result = [db executeUpdate:insertTableSql withArgumentsInArray:[self insertParam]];
    
    return result;

}


- (BOOL)updateInDatabase:(FMDatabase *)db
{
    NSString *updateTableSql = [NSString stringWithFormat:@"UPDATE %@ SET %@ = ?, %@ = ? WHERE %@ = ?",
                                [MessageRemind TableName],
                                [MessageRemind groupName],
                                [MessageRemind isRemind],
                                [MessageRemind groupID]];
    
    
    BOOL result = [db executeUpdate:updateTableSql withArgumentsInArray:[self updateParam]];
    
    return result;
}


+ (BOOL)deleteWithGroupID:(NSString *)groupID database:(FMDatabase *)db
{
    NSString *deleteTableSql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@ = ?",[MessageRemind TableName],[MessageRemind groupID]];

    BOOL result = [db executeUpdate:deleteTableSql,groupID];
    
    return result;
}

//根据是否进行提醒查询群/讨论组
+ (NSArray *)queryGroupRemindWithIsRemind:(NSInteger)isRemind database:(FMDatabase *)db
{
    NSString *queryGroupRemindSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?",
                                 [MessageRemind TableName],
                                 [MessageRemind isRemind]];
    FMResultSet *rs = [db executeQuery:queryGroupRemindSql,[NSNumber numberWithInteger:isRemind]];
    
    NSMutableArray *remindArray = [NSMutableArray array];
    
    while ([rs next])
    {
        MessageRemind *messageRemind = [[MessageRemind alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [messageRemind setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [remindArray addObject:messageRemind];
        
    }
    
    [rs close];
    
    return remindArray;
}

+ (MessageRemind *)queryGroupRemindWithGroupID:(NSString *)groupID database:(FMDatabase *)db
{
    NSString *queryGroupRemindSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?",
                                     [MessageRemind TableName],
                                     [MessageRemind groupID]];
    FMResultSet *rs = [db executeQuery:queryGroupRemindSql,groupID];
    
    MessageRemind *messageRemind;
    
    while ([rs next])
    {
        messageRemind = [[MessageRemind alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [messageRemind setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        
    }
    
    [rs close];
    
    return messageRemind;
  
}


+ (NSArray *)queryInDatabase:(FMDatabase *)db
{
    NSString *queryGroupRemindSql = [NSString stringWithFormat:@"SELECT * FROM %@",
                                     [MessageRemind TableName]];
    FMResultSet *rs = [db executeQuery:queryGroupRemindSql];
    
    NSMutableArray *remindArray = [NSMutableArray array];
    
    while ([rs next])
    {
        MessageRemind *messageRemind = [[MessageRemind alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [messageRemind setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [remindArray addObject:messageRemind];
        
    }
    
    [rs close];
    
    return remindArray;
}


//根据是否进行提醒查询群/讨论组
- (NSArray *)queryGroupRemindWithGroupIDInDatabase:(FMDatabase *)db
{
    NSString *queryGroupRemindSql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?",
                                     [MessageRemind TableName],
                                     [MessageRemind groupID]];
    FMResultSet *rs = [db executeQuery:queryGroupRemindSql,self.groupID];
    NSMutableArray *remindArray = [NSMutableArray array];
    
    while ([rs next])
    {
        MessageRemind *messageRemind = [[MessageRemind alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [messageRemind setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [remindArray addObject:messageRemind];
        
    }
    
    [rs close];
    
    return remindArray;
}

//根据属性isRemind进行联合查询，查询出群信息
+ (NSArray*)queryGroupRemind:(NSInteger)isRemind database:(FMDatabase *)db
{
    NSString *unitQuerySql = [NSString stringWithFormat:@"SELECT u.* FROM MessageRemind m,UserGroup u WHERE m.groupID = u.groupName AND m.isRemind = ?"];
    
    FMResultSet *rs = [db executeQuery:unitQuerySql,[NSNumber numberWithInteger:isRemind]];
    NSMutableArray *tempArray = [NSMutableArray array];
    
    while ([rs next])
    {
        YNContactGroup *contactGroup = [[YNContactGroup alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [contactGroup setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        
        [tempArray addObject:contactGroup];
    }
    
    [rs close];
    return tempArray;
}

+ (NSArray*)queryDisGroupRemind:(NSInteger)isRemind database:(FMDatabase *)db
{
    NSString *unitQuerySql = [NSString stringWithFormat:@"SELECT d.* FROM MessageRemind m,DiscussionGroup d WHERE m.groupID = d.groupName AND m.isRemind = ?"];
    
    FMResultSet *rs = [db executeQuery:unitQuerySql,[NSNumber numberWithInteger:isRemind]];
    NSMutableArray *tempArray = [NSMutableArray array];
    
    while ([rs next])
    {
        YNContactGroup *contactGroup = [[YNContactGroup alloc] init];
        if (![YNIMTool isEmptyDictionary:[rs resultDictionary]]) {
            [contactGroup setValuesForKeysWithDictionary:[rs resultDictionary]];
        }
        
        [tempArray addObject:contactGroup];
    }
    
    [rs close];
    return tempArray;
}

//判断群/讨论组消息提醒是否存在
- (BOOL)isMessageRemindExistInDatabase:(FMDatabase *)db
{
    NSArray *remindArray = [self queryGroupRemindWithGroupIDInDatabase:db];
    if (remindArray.count > 0)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
